//
//  Account.m
//  CMJWeibo
//
//  Created by BH on 15/12/9.
//
//

#import "Account.h"

@implementation Account

+(instancetype)accountWithDict:(NSDictionary *)dict
{
    Account *account = [[Account alloc] init];
    account.access_token = dict[@"access_token"];
    account.uid = dict[@"uid"];
    account.remind_in = dict[@"remind_in"];
    account.expire_in = dict[@"expires_in"];
    
    NSDate *now = [NSDate date];
    account.expire_time = [now dateByAddingTimeInterval:account.expire_in.doubleValue];
    
    return account;
}

/**
 *  当把一个对象 归档 到文件中 会调用一次
 */
- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.access_token forKey:@"access_token"];
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.remind_in forKey:@"remind_in"];
    [encoder encodeObject:self.expire_in forKey:@"expires_in"];
    [encoder encodeObject:self.expire_time forKey:@"expires_time"];
}

/**
 *  从文件中 解析 一个对象 时会调用1次
 */
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        self.name = [decoder decodeObjectForKey:@"name"];
        self.uid = [decoder decodeObjectForKey:@"uid"];
        self.remind_in = [decoder decodeObjectForKey:@"remind_in"];
        self.expire_in = [decoder decodeObjectForKey:@"expires_in"];
        self.expire_time = [decoder decodeObjectForKey:@"expires_time"];
    }
    return self;
}

@end
